package cn.mymall.service.mall.impl;

import cn.mymall.entity.Member;
import cn.mymall.entity.mall.Product;
import cn.mymall.entity.mall.ShippingRegionPrice;
import cn.mymall.entity.mall.ShippingTemplate;
import cn.mymall.entity.mall.Sku;
import cn.mymall.mapper.ProductMapper;
import cn.mymall.service.impl.BaseServiceImpl;
import cn.mymall.service.mall.IProductService;
import cn.mymall.service.mall.IShippingRegionPriceService;
import cn.mymall.service.mall.IShippingTemplateService;
import cn.mymall.util.DateUtil;
import cn.mymall.vo.product.ProductSearchVo;
import cn.mymall.vo.product.ShippingFeeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class ProductServiceImpl extends BaseServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private IShippingTemplateService shippingTemplateService;
    @Autowired
    private IShippingRegionPriceService shippingRegionPriceService;
    @Override
    public Page<Product> getProductList(ProductSearchVo searchVo) {
        QueryWrapper<Product> ew = new QueryWrapper<>();
        if (searchVo.getProductId() != null) {
            ew.eq("id", searchVo.getProductId());
        }
        if (StringUtils.isNotBlank(searchVo.getName())) {
            ew.like("name", searchVo.getName());
        }
        if (StringUtils.isNotBlank(searchVo.getStatus())) {
            ew.eq("status", searchVo.getStatus());
        }
        if (StringUtils.isNotBlank(searchVo.getFlag())) {
            ew.like("flag", searchVo.getFlag());
        }
        if (searchVo.getCategoryId() != null) {
            ew.eq("category_id", searchVo.getCategoryId());
        }
        if (searchVo.getSubCategoryId() != null) {
            ew.eq("sub_category_id", searchVo.getSubCategoryId());
        }
        if (searchVo.getShipperId() != null) {
            ew.eq("shipper_id", searchVo.getShipperId());
        }
        if (searchVo.getVendorId() != null) {
            ew.eq("vendor_id", searchVo.getVendorId());
        }
        if (searchVo.getUnExpired() != null && searchVo.getUnExpired()) {
            ew.ge("off_sale_time", DateUtil.getCurFullTimestamp());
        }
        if(CollectionUtils.isNotEmpty(searchVo.getProductIdList())){
            ew.in("id", searchVo.getProductIdList());
        }
        if(CollectionUtils.isNotEmpty(searchVo.getExcludeCategoryIdList())){
            if(searchVo.getExcludeCategoryIdList().size()==1){
                ew.ne("category_id", searchVo.getExcludeCategoryIdList().get(0));
            }else {
                ew.notIn("category_id", searchVo.getExcludeCategoryIdList());
            }
        }
        if(StringUtils.isNotBlank(searchVo.getSortField())){
            if(searchVo.getAsc()!=null && searchVo.getAsc()) {
                ew.orderByAsc(searchVo.getSortField());
                ew.orderByDesc("id");
            }else {
                ew.orderByDesc(searchVo.getSortField());
            }
        }else {
            ew.orderByDesc("id");
        }
        Page page = page(new Page(searchVo.getPageNo(), searchVo.getPageSize()), ew);
        return page;
    }

    @Override
    public ShippingFeeVo getShippingFeeByProduct(Member member, Product product, Sku sku, int qty, Integer provinceId) {
        Integer templateId = product.getShippingTemplateId();
        ShippingTemplate template = shippingTemplateService.getById(templateId);

        ShippingRegionPrice srp = shippingRegionPriceService.findByTemplateIdAndProvincecode(templateId, provinceId);
        if (srp != null) {
            return new ShippingFeeVo(srp.getPrice(), product, template);
        }
        BigDecimal skuAmount = sku.getSellPrice().multiply(new BigDecimal(qty));
        if(StringUtils.equalsIgnoreCase(template.getSupportExceedFree(), "Y")
                && skuAmount.compareTo(new BigDecimal(100))>=0) {
            return new ShippingFeeVo(BigDecimal.ZERO, product, template);
        }
        if (template.getExceedPrice() != null && template.getExceedAfterPrice() != null
                && skuAmount.compareTo(template.getExceedPrice()) >= 0) {
            return new ShippingFeeVo(template.getExceedAfterPrice(), product, template);
        }
        return new ShippingFeeVo(template.getPrice(), product, template);
    }

}
